package leetCode;

import java.util.List;

/**
 * 单链表反转
 */
public class ListReverse {

    public static void main(String[] args) {
        int[] nums = new int[]{2, 7, 11, 15};
        int[] ints = twoSum(nums, 9);
        System.out.println(ints[0] + "," + ints[1]);
    }

    /**
     * 反转链表
     *
     * @param head
     * @return
     */
    public static ListNode reverseList(ListNode head) {

        if (head == null ) return null;

        ListNode pre = null;
        ListNode cur = head;

        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;

    }

    /**
     * 反转链表 - 递归
     *
     * @param head
     * @return
     */
    public static ListNode reverseListDG(ListNode head) {

        if (head == null || head.next ==null) return head;

        ListNode reverse = reverseListDG(head.next);
        head.next.next = head;
        head.next = null;
        return reverse;
    }

    /**
     * 两数之和
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        if (nums == null) return null;

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    /**
     * 整数反转
     *
     * @param x
     * @return
     */
    public static int reverse(int x) {

        long rev = 0;

        while (x != 0) {

            int pop = x % 10;
            rev = rev * 10 + pop;

            if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) return 0;

            
            x /= 10;

        }
        return (int) rev;
    }


}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}
